package com.itheima.util;

import java.util.ArrayList;
import java.util.Random;

public class BallUtil {
    private static Random r = new Random();
    private ArrayList<Integer> redPool = new ArrayList<>();
    private ArrayList<Integer> bluePool = new ArrayList<>();

    {
        for (int i = 1; i <= 33; i++) {
            redPool.add(i);
        }
        for (int i = 1; i <= 16; i++) {
            bluePool.add(i);
        }
    }

    private Integer getRedBall() {
        int index = r.nextInt(redPool.size());
        return redPool.get(index);
    }

    public Integer getBlueBall() {
        int index = r.nextInt(bluePool.size());
        return bluePool.get(index);
    }

    public ArrayList<Integer> getRedBalls() {
        ArrayList<Integer> redBalls = new ArrayList<>();
        for (int i = 0; i < 6; ) {
            Integer redBall = getRedBall();
            if(i>3){
                boolean exists_3 = isExists(redBall - 3, redBalls);
                boolean exists_2 = isExists(redBall - 2, redBalls);
                boolean exists_1 = isExists(redBall - 1, redBalls);
                boolean exists1_ = isExists(redBall +1, redBalls);
                boolean exists2_ = isExists(redBall +2, redBalls);
                boolean exists3_ = isExists(redBall +3, redBalls);
                if(exists_3&&exists_2&&exists_1){
                    continue;
                }
                if(exists_2&&exists_1&&exists1_){
                    continue;
                }
                if(exists_1&&exists1_&&exists2_){
                    continue;
                }
                if(exists1_&&exists2_&&exists3_){
                    continue;
                }
            }
            redPool.remove(redBall);
            redBalls.add(redBall);
            i++;
        }
        return redBalls;
    }
    //定义一个方法 判断是否包含指定的球
    public boolean isExists(Integer ball ,ArrayList<Integer> balls){
        for (Integer integer : balls) {
            if(ball==integer){
                return true;
            }
        }
        return false;
    }



}
